import joinRules from '@/pages/Drag/sub-components/previewModal/joinRules';

export default function fieldShowRule(id:string,rules:any[],formData:any[]){
  // console.log('rules',rules);

  // 1. 从rules查找是否有需要设置显隐规则的组件
  let filter = null;
  for(let i=0;i<rules.length;i++) {
    const item = rules[i];
    const has = item.fields.some((cid:any) => cid===id);
    if(has) {
      filter = item.filter
      break;
    }
  }

  // console.log('filter',filter);

   //如果没有则不需要校验显隐规则
  if(!filter) return true; 

  //多条规则校验，返回布尔值数组
  const checks = filter.cond.map((item:any) =>{
    const compData = formData.find((v:any)=>v.id === item.field);
    const {compared} = joinRules[item.method];
    console.log({ruleVal:item.value,compDataVal:compData.value})
    // return true
    return compared(compData.value,item.value);
  })
  // console.log('checks',checks);
  
  //如果全部为 true 则通过校验
  const show = checks.every((v:boolean)=>v);

  // console.log('showComp',show);

  return show;
}